package org.dftproject.genesis.ui;

import org.dftproject.genesis.core.collections.Collection;
import org.dftproject.genesis.core.collections.CollectionManager;
import org.dftproject.genesis.core.collections.ICollectionManagerListener;
import org.dftproject.genesis.core.data.flow.DataBroker;
import org.dftproject.genesis.core.tasks.ITask;
import org.dftproject.genesis.core.tasks.TaskManager;
import org.dftproject.genesis.ui.pages.home.HomePage;
import org.dftproject.genesis.ui.tasks.NavigationTaskHandler;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {

	private static final String GET_STARTED_TASK = "org.dftproject.genesis.ui.tasks.getStarted";

	public static final String ID = "org.dftproject.genesis.ui";
	
	public static final String PAGE_CONTEXT = ID + ".contexts.page";
	
	public static final String ADD_COLLECTION_PAGES_EXTENSION_POINT = "addCollectionPages";
	public static final String ADD_ONLINE_COLLECTION_PAGES_EXTENSION_POINT = "addOnlineCollectionPages";
	public static final String COLLECTION_HANDLERS_EXTENSION_POINT = "collectionHandlers";
	public static final String CITATION_FORMATTERS_EXTENSION_POINT = "citationFormatters";
	public static final String TASK_HANDLERS_EXTENSION_POINT = "taskHandlers";

	private static Activator plugin;

	public Activator() {
	}

	@Override
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
		
		createResources();
		
		CollectionManager.getDefault().addListener(new ICollectionManagerListener() {

			public void collectionAdded(Collection collection) {
				if (!CollectionManager.getDefault().getCollections().isEmpty()) {
					ITask task = TaskManager.getDefault().getTask(GET_STARTED_TASK);
					if (task != null)
						TaskManager.getDefault().removeTask(task);
				}
			}

			public void collectionRemoved(Collection collection) {
				if (CollectionManager.getDefault().getCollections().isEmpty()) {
					ITask task = TaskManager.getDefault().getTask(GET_STARTED_TASK);
					if (task == null) {
						TaskManager.getDefault().addTask(new ITask() {
	
							public String getId() {
								return GET_STARTED_TASK;
							}

							public String getType() {
								return NavigationTaskHandler.NAVIGATION_TASK_TYPE;
							}
	
							public String getLabel() {
								return "Get started!";
							}

							public String getAttribute(String name) {
								if ("navigation".equals(name))
									return "openPage";
								if ("pageId".equals(name))
									return HomePage.ID;
								return null;
							}
							
						});
					}
				}
			}

			public void collectionUpdated(Collection collection) {
			}
			
		});
		
		if (CollectionManager.getDefault().getCollections().isEmpty()) {
			ITask task = TaskManager.getDefault().getTask(GET_STARTED_TASK);
			if (task == null) {
				TaskManager.getDefault().addTask(new ITask() {

					public String getId() {
						return GET_STARTED_TASK;
					}

					public String getType() {
						return NavigationTaskHandler.NAVIGATION_TASK_TYPE;
					}

					public String getLabel() {
						return "Get started!";
					}

					public String getAttribute(String name) {
						if ("navigation".equals(name))
							return "openPage";
						if ("pageId".equals(name))
							return HomePage.ID;
						return null;
					}
					
				});
			}
		}
	}

	protected void createResources() {
		Display.getDefault().asyncExec(new Runnable() {

			public void run() {
				Font initialFont = JFaceResources.getDefaultFont();
				FontData[] fontData = initialFont.getFontData();
				for (int i = 0; i < fontData.length; i++)
					fontData[i].setHeight((fontData[i].getHeight() * 2));
				JFaceResources.getFontRegistry().put(GenesisResources.H1_FONT, fontData);
			}
			
		});
	}

	@Override
	public void stop(BundleContext context) throws Exception {
		DataBroker.getDefault().stop();
		
		plugin = null;
		super.stop(context);
	}

	public static Activator getDefault() {
		return plugin;
	}

	public static ImageDescriptor getImageDescriptor(String path) {
		return imageDescriptorFromPlugin(ID, path);
	}
	
	@Override
	public void initializeImageRegistry(ImageRegistry reg) {
		reg.put(SharedImages.IMG_MALE, getImageDescriptor("icons/male.png"));
		reg.put(SharedImages.IMG_FEMALE, getImageDescriptor("icons/female.png"));
		reg.put(SharedImages.IMG_NULL, getImageDescriptor("icons/null.png"));
		reg.put(SharedImages.IMG_WORLD, getImageDescriptor("icons/world.png"));
		reg.put(SharedImages.IMG_WARNING, getImageDescriptor("icons/error.png"));
	}
	
}
